#ifndef _MYSQLCONNECT_H_
#define _MYSQLCONNECT_H_
#include <mysql/mysql.h>
#include <oal.h>
class mysqlconnect
{
    private:
    MYSQL       *mysql;
     // 结果集
    MYSQL_RES   *m_pMysqlRes;
    // 行
    MYSQL_ROW   m_row;
    // 列
    MYSQL_FIELD *m_pMysqlField;
    oal_bool    m_bInit;

    public:
    string  mHost;
    string  mUser;
    string  mPassword;
    string  mDatabase;
    mysqlconnect()
    {
        mysql           = oal_ptr_null;
        m_pMysqlRes     = oal_ptr_null;
        m_pMysqlField   = oal_ptr_null;
        m_bInit         = false;
    }
    oal_bool Init(string& host, string& user, string& passwd, string& database)
    {
        mHost = host;
        mUser = user;
        mPassword = passwd;
        mDatabase = database;
        mysql = mysql_init(NULL);
        if(mysql == NULL)
        {
             oal_print("mysql Init 444\n!!");
            return false;
        }
       // oal_print("mysql Init 555\n!!");
        if(mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), database.c_str(), 0, NULL, 0) != NULL)
        {
            m_bInit = true;

            //oal_print("mysql query start , the mysql addr [%p]\n!!",mysql);
            //mysql_query(mysql, "SELECT * FROM user");
            //oal_print("mysql query end\n!!");
            return true;
        }
        else
        {
            oal_print("mysql Init 666\n!!");
            m_bInit = false;
            return false;
        }
    }
    oal_int32 query(string sql_s)
    {
        if(mysql)
        {
            oal_print("query, the mysql addr [%p]\n",mysql);
            // return mysql_query(mysql, sql_s.c_str());
            return mysql_query(mysql, "SELECT * FROM user");
        }
        else
        {
            oal_print("mysql null");
            return 1;
        }
        
    }
    MYSQL_RES* store_result()
    {
        return mysql_store_result(mysql);
    }
    oal_uint32 affected_rows()
    {
        return mysql_affected_rows(mysql);
    }
    oal_void free_result(MYSQL_RES *result)
    {
        mysql_free_result(result);
    }
    MYSQL_FIELD *fetch_field(MYSQL_RES *result)
    {
        return mysql_fetch_field(result);
    }
    MYSQL_ROW  fetch_row(MYSQL_RES *result)
    {
        return mysql_fetch_row(result);
    }
    oal_uint32 fields_num(MYSQL_RES *result)
    {
        return mysql_num_fields(result);
    }
    oal_uint32 rows_num(MYSQL_RES *result)
    {
        return mysql_num_rows(result);
    }
    oal_void close()
    {
        mysql_close(mysql);
    }
};

#endif